package com.zyg.controller;
import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zyg.dao.UserMapper;
import com.zyg.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.zyg.pojo.User;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

    @SaIgnore
    @GetMapping("/")
    public String login() {
        return "login";
    }

    @PostMapping("/login")
    public String login(@RequestParam String name, @RequestParam String identificationNumber,@RequestParam("captcha") String userCaptcha, HttpServletRequest request) throws IOException {
        String sessionCaptcha = (String) request.getSession().getAttribute("captcha");
        // 验证验证码
        if (sessionCaptcha == null || !sessionCaptcha.equalsIgnoreCase(userCaptcha)) {
            request.setAttribute("error", "验证码错误");
            return "login"; // 返回登录页面
        }else{
        // 查询数据库
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name).eq("identification_number", identificationNumber);

        User user = userMapper.selectOne(queryWrapper);
        if (user != null) {
            return "index";
        } else {
            // 登录失败
            return "error";
        }}
    }

    @GetMapping("/captcha")
    public void generateCaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int width = 120;
        int height = 40;
        int captchaLength = 5;

        // 创建图片
        BufferedImage captchaImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = captchaImage.createGraphics();

        // 设置背景色
        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(0, 0, width, height);

        // 设置字体
        g.setFont(new Font("Arial", Font.BOLD, 20));

        // 随机生成验证码
        String captchaText = generateRandomText(captchaLength);
        request.getSession().setAttribute("captcha", captchaText);

        // 绘制验证码字符
        Random random = new Random();
        for (int i = 0; i < captchaLength; i++) {
            g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
            g.drawString(String.valueOf(captchaText.charAt(i)), 20 * i + 10, 25);
        }

        // 添加一些干扰线
        for (int i = 0; i < 5; i++) {
            g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
            g.drawLine(random.nextInt(width), random.nextInt(height), random.nextInt(width), random.nextInt(height));
        }

        g.dispose();

        // 输出图片到响应
        response.setContentType("image/png");
        ImageIO.write(captchaImage, "png", response.getOutputStream());
    }

    private String generateRandomText(int length) {
        String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(chars.charAt(random.nextInt(chars.length())));
        }
        return sb.toString();
    }

}



